package com.mycar.pajero.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


/**
 * @desc: 跨域请求配置
 **/
@Configuration
public class CORSConfiguration {

    @Bean
    public FilterRegistrationBean corsFilter(){
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        CorsConfiguration config=new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");// 1允许任何域名使用
        config.addAllowedHeader("*");// 2允许任何头
        config.addAllowedMethod("*");// 3允许任何方法（post、get等）
        config.setMaxAge(1800L);// 4.解决跨域请求两次，预检请求的有效期，单位为秒
        source.registerCorsConfiguration("/**",config);
        FilterRegistrationBean bean=new FilterRegistrationBean(new CorsFilter(source));
        return bean;
    }
}